<?php
namespace app\system\controller;

use app\common\model\TradeModel;
use think\Db;

class TradeController extends BaseController
{
    private $tradeCache;
    private $tradeModel;
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->tradeModel = new TradeModel();
        $this->tradeCache = cache('trade');
    }

    /**
     * 行业首页
     * @return mixed
     */
    public function index()
    {
        $this->assign('tradeList',$this->tradeCache);
        return $this->fetch();
    }


    /**
     * 添加行业
     */
    public function add()
    {
        if($this->request->isAjax()){
            $data = $this->request->param();
            $result = $this->validate($data,'TradeValidate');
            if($result !== true){
                $this->error($result);
            }
            if($this->tradeModel->addData($data)){
                $this->tradeModel->updateCache();
                $this->success('添加成功！',url('index'));
            }else{
                $this->error($this->tradeModel->getError());
            }
        }
        $this->assign('tradeList',$this->tradeCache);
        return $this->fetch();
    }

    /**
     * 编辑行业
     * @return mixed
     */
    public function edit(){
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'TradeValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $this->TradeModel->editData($data);
            $this->TradeModel->updateCache();
            $this->success('操作成功！', url('index'));
        }
        $tradeId = $this->request->param('trade_id');
        $info = $this->tradeCache[$tradeId];
        foreach ($this->tradeCache as $k => $v) {
            //父栏目select状态
            $v['selected'] = $info['trade_pid'] == $v['trade_id'] ? 'selected' : '';
            //子栏目disabled
            $disabled      = \org\util\TreeUtil::isChild($this->tradeCache, $v['trade_id'], $tradeId, 'trade_id', 'trade_pid') || $tradeId == $v['trade_id'] ? 'disabled' : '';
            $v['disabled'] = $disabled;
            $tradeList[]   = $v;
        }
        $this->assign('info',$info);
        $this->assign('tradeList',$tradeList);
        return $this->fetch();
    }
    /**
     * 删除行业
     */
    public function del()
    {
        $tradeId = $this->request->param('trade_id');
        $tradeInfo = $this->tradeCache[$tradeId];
        if($tradeInfo['is_son'] == true){
            $this->error('请先删除该行业下的子行业');
        }else{
            $this->tradeModel->delData($tradeId);
            $this->tradeModel->updateCache();
            $this->success('删除成功！');
        }
        return $this->fetch();
    }

    /**
     * 更新排序
     */
    public function updateOrder() {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $trade_id => $trade_sort) {
            $tradeId    = intval($trade_id);
            $tradeSort  = intval($trade_sort);
            $dataList[] = ["trade_id" => $tradeId, "trade_sort" => $tradeSort];
        }
        $this->tradeModel->saveAll($dataList);
        $this->updateCache();
        $this->success('排序成功！');
    }

    /**
     * 更新缓存
     */
    public function updateCache(){
        if($this->tradeModel->updateCache()){
            $this->success('更新成功！');
        }
    }

}
